package mariangela;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JOptionPane;

public class VisualFrame implements KeyListener {

    private int velocità = 30;
    long tempo = System.currentTimeMillis() * 1000;
    private int danniUserAuto;
    public static boolean pause = false;
    static boolean inUseKey = false;
    private JFrame frame;
    public static Figure autoUser;
    private Figure autoPC;
    public static final Dimension XYFrame = new Dimension(768, 680);
    public static int larghezzaFrame = XYFrame.width;
    public static int altezzaFrame = XYFrame.height - 200;
    public static Point xy1 = new Point(larghezzaFrame / 2, (altezzaFrame / 2) + altezzaFrame);
    public static Point xy2 = new Point(larghezzaFrame - 30, altezzaFrame);
    private JPanel pan;
    private JButton pausa;
    public MoveCars user;
    private MoveCars PC;
    private JLabel sfondo;
    private JButton turboB;
    private Thread move;

    public VisualFrame() {
        impFrm();
        JPanel finestra = new JPanel();
        finestra.setOpaque(true);
        finestra.setSize(XYFrame);
        finestra.setBackground(new Color(26, 8, 91));
        finestra.setLayout(null);
        frame.setContentPane(finestra);
        setPush();
        impFigAndPiano();
        setMoveAutoPC();
        pulsantePausa();
        frame.setVisible(true);
    }

    private void impFigAndPiano() {
        this.sfondo = new JLabel();
        this.sfondo.setSize(larghezzaFrame - 8, altezzaFrame);
        this.sfondo.setLocation(0, 0);
        this.sfondo.setIcon(createImageIcon("Strada.jpg"));
        this.sfondo.setToolTipText("Mariangela's MiniRally");
        autoUser = new Figure(true);
        this.autoPC = new Figure(false);
        autoUser.setLocation(xy1);
        this.autoPC.setLocation(xy2);
        this.pan = new JPanel();
        this.pan.setSize(larghezzaFrame, altezzaFrame);
        this.pan.setLocation(1, 1);
        this.pan.setLayout(null);
        this.pan.add(autoUser);
        this.pan.add(this.autoPC);
        this.pan.add(this.sfondo);
        this.frame.add(this.pan);
    }

    private void impFrm() {
        frame = new JFrame("MiniRally 1.0 By Mariangela");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(XYFrame);
        frame.setLocation(100, 100);
        frame.setResizable(false);
        frame.setAlwaysOnTop(true);
        frame.addKeyListener(this);
        frame.setFocusable(true);
    }

    protected static ImageIcon createImageIcon(String percorso) {
        java.net.URL img = mariangela.VisualFrame.class.getResource(percorso);
        if (img != null) {
            return new ImageIcon(img);
        } else {
            return null;
        }
    }

    private void setPush() {
        frame.add(new TastiToMove(50, 30, "left", new Point(XYFrame.width / 2 - 55 - 10, XYFrame.height - 200 + 45 + 40)));
        frame.add(new TastiToMove(50, 30, "right", new Point(XYFrame.width / 2 + 5 + 10, XYFrame.height - 200 + 45 + 40)));
    }

    private void setMoveAutoPC() {
        class ThreadAutoPC extends Thread {

            @Override
            public void run() {
                String movimentoPC = "down";
                PC = new MoveCars(movimentoPC, xy2, autoPC, velocità);
                PC.start();
                while (System.currentTimeMillis() * 1000 - tempo == 30) {
                    tempo = System.currentTimeMillis() * 1000;
                    velocità++;
                }
                //  if (scontro()){

                if (fineGioco()) {
                    user.stop();
                    PC.stop();
                    sfondo.setIcon(createImageIcon("incidenteFatale.jpg"));
                    pause = true;
                    pausa.setEnabled(false);
                    turboB.setEnabled(false);
                    JOptionPane.showMessageDialog(null, "Incidente fatale... GameOver");
                }
            }
        }
        new ThreadAutoPC().start();
    }

    private boolean scontro() {
        this.danniUserAuto++;
        return distanza() <= 9;
    }

    private boolean fineGioco() {
        if (this.danniUserAuto == 10) {
            return true;
        }
        return false;
    }

    private double distanza() {
        return Math.sqrt(Math.pow(xy1.x - xy2.x, 2) + Math.pow(xy1.y - xy2.y, 2));
    }

    private void pulsantePausa() {
        pausa = new JButton("Pausa");
        pausa.setSize(larghezzaFrame - 8, 25);
        pausa.setLocation(1, altezzaFrame + 2);
        class messaInPausa implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                if (!pause) {
                    pausa.setText("Pausa");
                    pause = false;
                    if (!fineGioco()) {
                        user.resume();
                        PC.resume();
                        pausa.setFocusable(false);
                    }
                } else {
                    pausa.setText("Avvia");
                    pause = true;
                    if (!fineGioco()) {
                        user.suspend();
                        PC.suspend();
                    }
                }
            }
        }
        ActionListener list3 = new messaInPausa();
        this.pausa.addActionListener(list3);
        this.frame.add(pausa);
    }

    public void keyTyped(KeyEvent e) {
        if (!TastiToMove.inUseMouse && !pause) {
            inUseKey = true;
            String azioneMovimento;
            switch (Character.toLowerCase(e.getKeyChar())) {
                case 'a':
                    azioneMovimento = "left";
                    break;
                case 's':
                    azioneMovimento = "right";
                    break;
                default:
                    azioneMovimento = "null";
                    break;
            }
            if (((this.move == null)) && !azioneMovimento.equals("null")) {
                TastiToMove.direzione = azioneMovimento;
                this.move = new MoveCars(azioneMovimento, xy1, autoUser, 15);
                this.move.start();
            }
        }
    }

    public void keyPressed(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {
        if (move != null && move.isAlive()) {
            TastiToMove.direzione = "";
            move.stop();
            move = null;
            inUseKey = false;
        }
    }
}
